package com.xxgc.springboot.config;

import com.xxgc.springboot.entity.TaskInfo;
import com.xxgc.springboot.service.ITaskInfoService;
import com.xxgc.springboot.util.QuartzUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 *
 * @program: springboot
 * @description:
 * @author: Mr.abel(ShiJiaYi)
 * @create: 2022-09-21 14:46
 **/
@Slf4j
@Component
public class QuartzSystemInit{
    @Autowired
    private ITaskInfoService iis;
    //在项目启动后执行
    @PostConstruct
    public void init(){
        //查询所有没有删除的任务
        List<TaskInfo> list = iis.list();
        for (TaskInfo taskInfo : list) {
            try {
                log.info("任务装配"+taskInfo.getDetailGroup()+":"+taskInfo.getDetailName());
                QuartzUtil.addJob(
                        taskInfo.getDetailName(),
                        taskInfo.getDetailGroup(),
                        taskInfo.getTriggerName(),
                        taskInfo.getTriggerGroup(),
                        taskInfo.getCron(),
                        (Class<? extends Job>) Class.forName(taskInfo.getDetailJob()));
            }catch (Exception e){
                //e.printStackTrace();
                log.error("项目启动时装配任务错误"+e.getMessage());
            }
        }
    }
}
